package co.yixiang.yshop.module.order.dal.mysql.storeorder;

import cn.hutool.core.util.StrUtil;
import co.yixiang.yshop.framework.common.enums.OrderInfoEnum;
import co.yixiang.yshop.framework.common.enums.ShopCommonEnum;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.mybatis.core.mapper.BaseMapperX;
import co.yixiang.yshop.framework.mybatis.core.query.LambdaQueryWrapperX;
import co.yixiang.yshop.module.member.dal.dataobject.burypoint.TrendChartRespVO;
import co.yixiang.yshop.module.order.controller.admin.storeorder.vo.StoreOrderExportReqVO;
import co.yixiang.yshop.module.order.controller.admin.storeorder.vo.StoreOrderPageReqVO;
import co.yixiang.yshop.module.order.dal.dataobject.storeorder.StoreOrderDO;
import co.yixiang.yshop.module.order.enums.AdminOrderStatusEnum;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 订单 Mapper
 *
 * @author yshop
 */
@Mapper
public interface StoreOrderMapper extends BaseMapperX<StoreOrderDO> {
    default MPJLambdaWrapper<StoreOrderDO> getOrderJoinPageWrapper(StoreOrderPageReqVO reqVO) {
        MPJLambdaWrapper<StoreOrderDO> wrapper = new MPJLambdaWrapper<>();
        if (reqVO.getCreateTime() != null) {
            wrapper.between(StoreOrderDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
        }
        wrapper
                .eq(ObjectUtils.isNotNull(reqVO.getOrderId()), StoreOrderDO::getOrderId, reqVO.getOrderId())
                .like(ObjectUtils.isNotNull(reqVO.getRealName()), StoreOrderDO::getRealName, reqVO.getRealName())
                .eq(ObjectUtils.isNotNull(reqVO.getUserPhone()), StoreOrderDO::getUserPhone, reqVO.getUserPhone())
                .eq(ObjectUtils.isNotNull(reqVO.getShippingType()), StoreOrderDO::getShippingType, reqVO.getShippingType())
                .eq(ObjectUtils.isNotNull(reqVO.getCampaignType()), StoreOrderDO::getCampaignType, reqVO.getCampaignType())
                .eq(ObjectUtils.isNotNull(reqVO.getWriteOffStatus()), StoreOrderDO::getWriteOffStatus, reqVO.getWriteOffStatus())
                .eq(ObjectUtils.isNotNull(reqVO.getStoreId()), StoreOrderDO::getStoreId, reqVO.getStoreId())
                .eq(ObjectUtils.isNotNull(reqVO.getPayStatus()), StoreOrderDO::getPayType, reqVO.getPayStatus())
                .orderByDesc(StoreOrderDO::getId);
        return wrapper;
    }


    default PageResult<StoreOrderDO> selectPage(StoreOrderPageReqVO reqVO) {
        LambdaQueryWrapperX<StoreOrderDO> wrapper = new LambdaQueryWrapperX();
        wrapper
                .eqIfPresent(StoreOrderDO::getOrderId, reqVO.getOrderId())
                .likeIfPresent(StoreOrderDO::getRealName, reqVO.getRealName())
                .eqIfPresent(StoreOrderDO::getUserPhone, reqVO.getUserPhone())
                .eqIfPresent(StoreOrderDO::getShippingType, reqVO.getShippingType())
                .eqIfPresent(StoreOrderDO::getCampaignType, reqVO.getCampaignType())
                .eqIfPresent(StoreOrderDO::getWriteOffStatus, reqVO.getWriteOffStatus())
                .eqIfPresent(StoreOrderDO::getStoreId, reqVO.getStoreId())
                .betweenIfPresent(StoreOrderDO::getCreateTime, reqVO.getCreateTime())
                //混合搜索
                .and(x -> x
                        .like(ObjectUtils.isNotNull(reqVO.getSearch()), StoreOrderDO::getRealName, reqVO.getSearch())
                        .or()
                        .like(ObjectUtils.isNotNull(reqVO.getSearch()), StoreOrderDO::getUid, reqVO.getSearch()))
                .orderByDesc(StoreOrderDO::getId);
        if (reqVO.getOrderStatus() != null) {
            switch (AdminOrderStatusEnum.toType(reqVO.getOrderStatus())) {
                //未支付
                case STATUS_0:
                    wrapper.eq(StoreOrderDO::getPaid, OrderInfoEnum.PAY_STATUS_UNPAID.getValue())
                            .eq(StoreOrderDO::getRefundStatus, OrderInfoEnum.REFUND_STATUS_NORMAL.getValue())
                            .eq(StoreOrderDO::getStatus, OrderInfoEnum.STATUS_DEFAULT.getValue());
                    break;
                //待发货
                case STATUS_1:
                    wrapper.eq(StoreOrderDO::getPaid, OrderInfoEnum.PAY_STATUS_HAVE_PAID.getValue())
                            .eq(StoreOrderDO::getRefundStatus, OrderInfoEnum.REFUND_STATUS_NORMAL.getValue())
                            .eq(StoreOrderDO::getStatus, OrderInfoEnum.STATUS_DEFAULT.getValue());
                    break;
                //待收货
                case STATUS_2:
                    wrapper.eq(StoreOrderDO::getPaid, OrderInfoEnum.PAY_STATUS_HAVE_PAID.getValue())
                            .eq(StoreOrderDO::getRefundStatus, OrderInfoEnum.REFUND_STATUS_NORMAL.getValue())
                            .eq(StoreOrderDO::getStatus, OrderInfoEnum.STATUS_WAIT_RECEIVED.getValue());
                    break;
                //待评价
                case STATUS_3:
                    wrapper.eq(StoreOrderDO::getPaid, OrderInfoEnum.PAY_STATUS_HAVE_PAID.getValue())
                            .eq(StoreOrderDO::getRefundStatus, OrderInfoEnum.REFUND_STATUS_NORMAL.getValue())
                            .eq(StoreOrderDO::getStatus, OrderInfoEnum.STATUS_RECEIVED.getValue());
                    break;
                //已完成
                case STATUS_4:
                    wrapper.eq(StoreOrderDO::getPaid, OrderInfoEnum.PAY_STATUS_HAVE_PAID.getValue())
                            .eq(StoreOrderDO::getRefundStatus, OrderInfoEnum.REFUND_STATUS_NORMAL.getValue())
                            .eq(StoreOrderDO::getStatus, OrderInfoEnum.STATUS_FINISHED.getValue());
                    break;
                //已退款
                case STATUS_5:
                    wrapper.eq(StoreOrderDO::getPaid, OrderInfoEnum.PAY_STATUS_UNPAID.getValue())
                            .eq(StoreOrderDO::getRefundStatus, OrderInfoEnum.REFUND_STATUS_HAVE_REFUNDED.getValue());
                    break;
                //已删除
                case STATUS_6:
                    wrapper.eq(StoreOrderDO::getIsSystemDel, ShopCommonEnum.DELETE_1.getValue());
                    break;
                default:
            }
        }
        if (StrUtil.isNotEmpty(reqVO.getPayStatus())) {
            wrapper.eq(StoreOrderDO::getPayType, reqVO.getPayStatus());
        }
        return selectPage(reqVO, wrapper);
    }

    default List<StoreOrderDO> selectList(StoreOrderExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<StoreOrderDO>()
                .eqIfPresent(StoreOrderDO::getOrderId, reqVO.getOrderId())
                .likeIfPresent(StoreOrderDO::getRealName, reqVO.getRealName())
                .eqIfPresent(StoreOrderDO::getUserPhone, reqVO.getUserPhone())
                .eqIfPresent(StoreOrderDO::getUserAddress, reqVO.getUserAddress())
                .betweenIfPresent(StoreOrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(StoreOrderDO::getId));
    }


    @Select("select IFNULL(sum(pay_price),0) from yshop_store_order " +
            "where paid=1 and deleted=0 and refund_status=0 and uid=#{uid}")
    double sumPrice(@Param("uid") Long uid);

    @Select("SELECT IFNULL(sum(pay_price),0) " +
            " FROM yshop_store_order ${ew.customSqlSegment}")
    Double todayPrice(@Param(Constants.WRAPPER) Wrapper<StoreOrderDO> wrapper);

    @Select("select IFNULL(sum(pay_price),0)  from yshop_store_order " +
            "where refund_status=0 and deleted=0 and paid=1")
    Double sumTotalPrice();

    @Update("update yshop_store_order set is_after_sales = #{isAfterSales} where id = #{orderId}")
    void closeAfterSaleOrder(@Param("orderId") Long orderId, @Param("isAfterSales") Integer isAfterSales);

    List<TrendChartRespVO> getTrendChart();
}
